package com.hsylient.demo02;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUpdate {
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    @SuppressWarnings("all")
    public static void main(String[] args) {
        Statement statement = null;
        Connection connection= null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            statement = connection.createStatement();
            String sql = "update user set username = '真难呀' where id = 1007";
            int affectCount = statement.executeUpdate(sql);
            System.out.println(affectCount == 1 ? "更新成功" : "更新失败");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
